/*
 * Class: CustomerDAO
 * Created by: Mary Joy Pascual
 * Date Created: 06/16/2014
 * Company: Netsuite
*/
package com.netsuite.dao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPException;

import com.netsuite.adapter.CustomerAdapter;
import com.netsuite.constants.Constants;
import com.netsuite.helper.ErrorHelper;
import com.netsuite.helper.ErrorSOAPHelper;
import com.netsuite.logger.LoggerImp;
import com.netsuite.pojo.CustomerPOJO;
import com.netsuite.webservices.lists.relationships_2014_1.Customer;
import com.netsuite.webservices.lists.relationships_2014_1.CustomerSearch;
import com.netsuite.webservices.lists.relationships_2014_1.CustomerSearchAdvanced;
import com.netsuite.webservices.lists.relationships_2014_1.CustomerSearchRow;
import com.netsuite.webservices.lists.relationships_2014_1.types.CustomerStage;
import com.netsuite.webservices.platform.common_2014_1.CustomerSearchBasic;
import com.netsuite.webservices.platform.common_2014_1.CustomerSearchRowBasic;
import com.netsuite.webservices.platform.core_2014_1.Record;
import com.netsuite.webservices.platform.core_2014_1.RecordList;
import com.netsuite.webservices.platform.core_2014_1.RecordRef;
import com.netsuite.webservices.platform.core_2014_1.SearchBooleanField;
import com.netsuite.webservices.platform.core_2014_1.SearchColumnBooleanField;
import com.netsuite.webservices.platform.core_2014_1.SearchColumnSelectField;
import com.netsuite.webservices.platform.core_2014_1.SearchColumnStringField;
import com.netsuite.webservices.platform.core_2014_1.SearchEnumMultiSelectField;
import com.netsuite.webservices.platform.core_2014_1.SearchMultiSelectField;
import com.netsuite.webservices.platform.core_2014_1.SearchResult;
import com.netsuite.webservices.platform.core_2014_1.SearchStringField;
import com.netsuite.webservices.platform.core_2014_1.types.RecordType;
import com.netsuite.webservices.platform.core_2014_1.types.SearchEnumMultiSelectFieldOperator;
import com.netsuite.webservices.platform.core_2014_1.types.SearchMultiSelectFieldOperator;
import com.netsuite.webservices.platform.core_2014_1.types.SearchStringFieldOperator;
import com.netsuite.webservices.platform.faults_2014_1.ExceededRecordCountFault;
import com.netsuite.webservices.platform.faults_2014_1.ExceededRequestLimitFault;
import com.netsuite.webservices.platform.faults_2014_1.ExceededRequestSizeFault;
import com.netsuite.webservices.platform.faults_2014_1.ExceededUsageLimitFault;
import com.netsuite.webservices.platform.faults_2014_1.InvalidCredentialsFault;
import com.netsuite.webservices.platform.faults_2014_1.InvalidSessionFault;
import com.netsuite.webservices.platform.messages_2014_1.ReadResponse;
import com.netsuite.webservices.platform.messages_2014_1.WriteResponse;
import com.netsuite.webservices.platform_2014_1.NetSuitePortType;

public class CustomerDAO {
	
	private static String CLASS_NAME = CustomerDAO.class.getName();
	
	private NetSuitePortType _service = null;

	public CustomerDAO() throws ErrorHelper {
		try {
			MainDAO mainDAO = new MainDAO();
			_service = mainDAO.get_service();
		} catch (FileNotFoundException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_FNF+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_FNF+ e.getMessage());
		} catch (ServiceException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SE+ e.getMessage());
		} catch (IOException e) {
			LoggerImp.errorLog(CLASS_NAME,Constants.ERROR_IO+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_IO+ e.getMessage());
		} catch (SOAPException e) {
			LoggerImp.errorLog(CLASS_NAME,Constants.ERROR_SOAP+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SOAP+ e.getMessage());
		}

	}

	public List<CustomerPOJO> getCustomerList() throws ErrorHelper {
		
		LoggerImp.startLog(CLASS_NAME, "getCustomerList()");

		CustomerSearchBasic customerSearchBasic = new CustomerSearchBasic();
		customerSearchBasic.setIsInactive(new SearchBooleanField());
		customerSearchBasic.getIsInactive().setSearchValue(false);
		
		customerSearchBasic.setStage(new SearchEnumMultiSelectField());
		customerSearchBasic.getStage().setOperator(SearchEnumMultiSelectFieldOperator.anyOf);
		customerSearchBasic.getStage().setSearchValue(new String[1]);
		customerSearchBasic.getStage().setSearchValue(0, CustomerStage.__customer);
		
		CustomerSearchRowBasic cusColumnBasic = new CustomerSearchRowBasic();
		cusColumnBasic.setEntityId(new SearchColumnStringField[1]);
		cusColumnBasic.setEntityId(0, new SearchColumnStringField());
		cusColumnBasic.setIsPerson(new SearchColumnBooleanField[1]);
		cusColumnBasic.setIsPerson(0, new SearchColumnBooleanField());
		cusColumnBasic.setCompanyName(new SearchColumnStringField[1]);
		cusColumnBasic.setCompanyName(0, new SearchColumnStringField());
		cusColumnBasic.setFirstName(new SearchColumnStringField[1]);
		cusColumnBasic.setFirstName(0, new SearchColumnStringField());
		cusColumnBasic.setMiddleName(new SearchColumnStringField[1]);
		cusColumnBasic.setMiddleName(0, new SearchColumnStringField());
		cusColumnBasic.setLastName(new SearchColumnStringField[1]);
		cusColumnBasic.setLastName(0, new SearchColumnStringField());
		cusColumnBasic.setInternalId(new SearchColumnSelectField[1]);
		cusColumnBasic.setInternalId(0, new SearchColumnSelectField());
		cusColumnBasic.setExternalId(new SearchColumnSelectField[1]);
		cusColumnBasic.setExternalId(0, new SearchColumnSelectField());
		cusColumnBasic.setEntityStatus(new SearchColumnSelectField[1]);
		cusColumnBasic.setEntityStatus(0, new SearchColumnSelectField());
		cusColumnBasic.setSubsidiary(new SearchColumnSelectField[1]);
		cusColumnBasic.setSubsidiary(0, new SearchColumnSelectField());
		
		CustomerSearchAdvanced cusAdv = new CustomerSearchAdvanced();
		cusAdv.setCriteria(new CustomerSearch());
		cusAdv.getCriteria().setBasic(customerSearchBasic);
		cusAdv.setColumns(new CustomerSearchRow());
		cusAdv.getColumns().setBasic(cusColumnBasic);

		int page = 1;
		int totalPages = 0;
		SearchResult searchResult = null;
		String searchId = "";
		
		List<CustomerPOJO> customersList = new ArrayList<CustomerPOJO>();
		
		try {
			LoggerImp.debugLog(CLASS_NAME, "Processing Customer Record List....");
			do {
				if(page==1){
					searchResult =  _service.search(customerSearchBasic);
					searchId = searchResult.getSearchId();
					totalPages = searchResult.getTotalPages();
					page++;
				} else {
					searchResult = _service.searchMoreWithId(searchId, page++);
				}
				if(searchResult.getStatus().isIsSuccess()){
					RecordList rowList = searchResult.getRecordList();
					for (int i=0; i<rowList.getRecord().length; i++){
						Customer cus = (Customer) rowList.getRecord(i);
						customersList.add(CustomerAdapter.setCustomer(cus));
						LoggerImp.infoLog(CLASS_NAME, "Customer Id : " + cus.getInternalId() + " extracted.");;
					}
				} else {
					ErrorSOAPHelper.displayError(CLASS_NAME, searchResult.getStatus().getStatusDetail());
				}
			}while (page <= totalPages);
			LoggerImp.infoLog(CLASS_NAME, "Customer Records Found : " + customersList.size() + " records matching filter: Is Not Inactive / Stage = Customer ");
		} catch (ExceededRequestSizeFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "getCustomerList()");
		return customersList;
	}
	
	
//public List<CustomerPOJO> getCustomerList() throws ErrorHelper {
//		
//		Logger.startLog(CLASS_NAME, "getCustomerList()");
//
//		CustomerSearchBasic customerSearchBasic = new CustomerSearchBasic();
//		customerSearchBasic.setIsInactive(new SearchBooleanField());
//		customerSearchBasic.getIsInactive().setSearchValue(false);
//		
//		customerSearchBasic.setStage(new SearchEnumMultiSelectField());
//		customerSearchBasic.getStage().setOperator(SearchEnumMultiSelectFieldOperator.anyOf);
//		customerSearchBasic.getStage().setSearchValue(new String[1]);
//		customerSearchBasic.getStage().setSearchValue(0, CustomerStage.__customer);
//		
//		//TODO: SEARCH COLUMNS
//		
//		int page = 1;
//		int totalPages = 0;
//		SearchResult searchResult = null;
//		String searchId = "";
//		
//		List<CustomerPOJO> customersList = new ArrayList<CustomerPOJO>();
//		
//		try {
//			Logger.debugLog(CLASS_NAME, "Processing Customer Record List....");
//			do {
//				if(page==1){
//					searchResult =  _service.search(customerSearchBasic);
//					searchId = searchResult.getSearchId();
//					totalPages = searchResult.getTotalPages();
//					page++;
//				} else {
//					searchResult = _service.searchMoreWithId(searchId, page++);
//				}
//				if(searchResult.getStatus().isIsSuccess()){
//					for(Record rec : searchResult.getRecordList().getRecord()){
//						Customer cus = (Customer) rec;
//						customersList.add(CustomerAdapter.setCustomer(cus));
//						Logger.infoLog(CLASS_NAME, "Customer Id : " + cus.getInternalId() + " extracted.");
//					}
//				} else {
//					Helper.displayError(CLASS_NAME, searchResult.getStatus().getStatusDetail());
//				}
//			}while (page <= totalPages);
//			Logger.infoLog(CLASS_NAME, "Customer Records Found : " + customersList.size() + " records matching filter: Is Not Inactive / Stage = Customer ");
//		} catch (ExceededRequestSizeFault e) {
//			Logger.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
//			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
//		} catch (InvalidCredentialsFault e) {
//			Logger.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
//			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
//		} catch (ExceededUsageLimitFault e) {
//			Logger.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
//			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
//		} catch (ExceededRequestLimitFault e) {
//			Logger.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
//			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
//		} catch (ExceededRecordCountFault e) {
//			Logger.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
//			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
//		} catch (InvalidSessionFault e) {
//			Logger.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
//			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
//		} catch (RemoteException e) {
//			Logger.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
//			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
//		}
//		
//		Logger.endLog(CLASS_NAME, "getCustomerList()");
//		return customersList;
//	}
	
	public CustomerPOJO getCustomer(String internalId, String externalId) throws ErrorHelper{
		
		LoggerImp.startLog(CLASS_NAME, "getCustomer()");
		
		RecordRef cusRef = new RecordRef();
		
		cusRef.setType(RecordType.customer);
		cusRef.setInternalId(internalId);
		cusRef.setExternalId(externalId);
		
		CustomerPOJO cusPOJO = null;
		try {
			LoggerImp.debugLog(CLASS_NAME, "Processing Customer Record....");
			ReadResponse readResponse = _service.get(cusRef);
			
			if(readResponse.getStatus().isIsSuccess()){
				Customer cus = (Customer) readResponse.getRecord();
				cusPOJO = CustomerAdapter.setCustomer(cus);
				LoggerImp.infoLog(CLASS_NAME, "Customer Record Found");
			} else {
				ErrorSOAPHelper.displayError(CLASS_NAME, readResponse.getStatus().getStatusDetail());
			}
		} catch (ExceededRequestSizeFault e) {
				LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
				throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "getCustomer()");
		
		return cusPOJO;
		
	}
	
	public String addCustomer(CustomerPOJO customerPojo) throws ErrorHelper {
		
		LoggerImp.startLog(CLASS_NAME, "addCustomer()");
		
		String internalId = "";
		Customer customer = CustomerAdapter.setCustomer(customerPojo);
		try {
			LoggerImp.debugLog(CLASS_NAME, "Adding Customer Record....");
			WriteResponse writeResponse = _service.add(customer);
			if(!writeResponse.getStatus().isIsSuccess()){
				ErrorSOAPHelper.displayError(CLASS_NAME, writeResponse.getStatus().getStatusDetail());
	  		} else {
	  			RecordRef rec = (RecordRef)writeResponse.getBaseRef();
	  			internalId = rec.getInternalId();
	  			LoggerImp.infoLog(CLASS_NAME, "Customer Id#"+internalId+" successfully generated");
	  		}
		} catch (ExceededRequestSizeFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "addCustomer()");
		
  		return internalId;
	}
	
	public void updateCustomer(CustomerPOJO customerPojo) throws ErrorHelper{
		
		LoggerImp.startLog(CLASS_NAME, "updateCustomer()");
		
		Customer customer = CustomerAdapter.setCustomer(customerPojo);
		try {
			LoggerImp.debugLog(CLASS_NAME, "Updating Customer Record....");
			WriteResponse writeResponse = _service.update(customer);
			if(!writeResponse.getStatus().isIsSuccess()){
				ErrorSOAPHelper.displayError(CLASS_NAME, writeResponse.getStatus().getStatusDetail());
	  		} else {
	  			LoggerImp.debugLog(CLASS_NAME, "Customer Record succesfully updated!");
	  		}
			
		} catch (ExceededRequestSizeFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "updateCustomer()");
	
	}
	
	public void deleteCustomer(String internalId, String externalId) throws ErrorHelper{
		
		LoggerImp.startLog(CLASS_NAME, "deleteCustomer()");
		
		RecordRef cusRef = new RecordRef();
		
		cusRef.setType(RecordType.customer);
		cusRef.setInternalId(internalId);
		cusRef.setExternalId(externalId);
		
		try {
			LoggerImp.debugLog(CLASS_NAME, "Deleting Customer Record....");
			WriteResponse writeResponse = _service.delete(cusRef);
			if(!writeResponse.getStatus().isIsSuccess()){
				ErrorSOAPHelper.displayError(CLASS_NAME, writeResponse.getStatus().getStatusDetail());
			} else {
				LoggerImp.debugLog(CLASS_NAME, "Customer Record succesfully deleted!");
			}
		} catch (ExceededRequestSizeFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "deleteCustomer()");
	}
	
	public List<CustomerPOJO> searchCustomer(String id, String criteria) throws ErrorHelper{
		
		LoggerImp.startLog(CLASS_NAME, "searchCustomer()");

		CustomerSearchBasic customerSearchBasic = new CustomerSearchBasic();
		customerSearchBasic.setIsInactive(new SearchBooleanField());
		customerSearchBasic.getIsInactive().setSearchValue(false);
		
		customerSearchBasic.setStage(new SearchEnumMultiSelectField());
		customerSearchBasic.getStage().setOperator(SearchEnumMultiSelectFieldOperator.anyOf);
		customerSearchBasic.getStage().setSearchValue(new String[1]);
		customerSearchBasic.getStage().setSearchValue(0, CustomerStage.__customer);
		
		if(id.equalsIgnoreCase(Constants.NAME)){
			customerSearchBasic.setEntityId(new SearchStringField(criteria, SearchStringFieldOperator.contains));
		}
		
		if(id.equalsIgnoreCase(Constants.INTERNAL_ID)){
			customerSearchBasic.setInternalId(new SearchMultiSelectField());
			customerSearchBasic.getInternalId().setOperator(SearchMultiSelectFieldOperator.anyOf);
			customerSearchBasic.getInternalId().setSearchValue(new RecordRef[1]);
			RecordRef rec1 = new RecordRef();
			rec1.setInternalId(criteria);
			customerSearchBasic.getInternalId().setSearchValue(0, rec1);
		}
		
		int page = 1;
		int totalPages = 0;
		SearchResult searchResult = null;
		String searchId = "";
		
		List<CustomerPOJO> customersList = new ArrayList<CustomerPOJO>();
		
		try {
			LoggerImp.debugLog(CLASS_NAME, "Processing Customer Record List based on Search Criteria....");
			do {
				if(page==1){
					searchResult =  _service.search(customerSearchBasic);
					searchId = searchResult.getSearchId();
					totalPages = searchResult.getTotalPages();
					page++;
				} else {
					searchResult = _service.searchMoreWithId(searchId, page++);
				}
				if(searchResult.getStatus().isIsSuccess()){
					for(Record rec : searchResult.getRecordList().getRecord()){
						Customer cus = (Customer) rec;
						customersList.add(CustomerAdapter.setCustomer(cus));
						LoggerImp.infoLog(CLASS_NAME, "Customer Id : " + cus.getInternalId() + " extracted.");
					}
				} else {
					ErrorSOAPHelper.displayError(CLASS_NAME, searchResult.getStatus().getStatusDetail());
				}
			}while (page <= totalPages); 
			
			LoggerImp.infoLog(CLASS_NAME, "Customer Records Found : " + customersList.size() + " records matching filter: "+criteria);
			
		} catch (ExceededRequestSizeFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "searchCustomer()");

		return customersList;
	}
	
	public String getCustomerInternalId (String customerId) throws ErrorHelper {
		
		LoggerImp.startLog(CLASS_NAME, "getCustomerInternalId()");
		
		String id = "";
		CustomerSearchBasic cusSearch = new CustomerSearchBasic();
		cusSearch.setEntityId(new SearchStringField());
		cusSearch.getEntityId().setSearchValue(customerId);
		cusSearch.getEntityId().setOperator(SearchStringFieldOperator.contains);
		
		SearchResult searchResult;
		try {
			searchResult = _service.search(cusSearch);
			if(searchResult.getStatus().isIsSuccess()){
				for(Record rec : searchResult.getRecordList().getRecord()){
					Customer cus = (Customer) rec;
					id = cus.getInternalId();
				}
			}
		} catch (ExceededRequestSizeFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "getCustomerInternalId()");
		
		return id;
	}
	

}
